<?php
class Db_Image
{
    public static function total($condition=false) {
        static $totals;
        $key = hash('crc32b', $condition);
        if(!isset($totals[$key]))
            $totals[$key] = DB::count('images', $condition, __FILE__ . ' line ' . __LINE__);
        return $totals[$key];
    }

    public static function getImages($limit='', $condition='') {
        static $images;
        $key = hash('crc32b', $condition.$limit);
        if(!isset($images[$key])) {
            if($condition) $condition = 'WHERE '.$condition;
            $images[$key] =  DB::fetch_all_array("SELECT * FROM images $condition $limit", __FILE__ . ' line ' . __LINE__);
        }
        return $images[$key];
    }

    public static function selectImages($cols='*', $limit='', $condition='', $returnKey = true)
    {
        static $images;
        $key = hash('crc32b', $cols.$condition.$limit);

        if(!isset($images[$key]))
        {
            if($condition)
            {
            	$condition = 'WHERE '.$condition;
            }
            if($returnKey){
                $images[$key] =  DB::fetch_all("SELECT $cols FROM images $condition $limit", __FILE__ . ' line ' . __LINE__);
            }else{
                $images[$key] =  DB::fetch_all_array("SELECT $cols FROM images $condition $limit", __FILE__ . ' line ' . __LINE__);
            }

        }

        return $images[$key];
    }
    public static function getImagesById($id){
        static $images;
        if(!isset($images[$id])){
            $arrId = explode(',',$id);
            if(sizeof($arrId) >1){
                $images[$id] = DB::select('images', 'id IN ('.$id.')', __FILE__ . ' line ' . __LINE__);
            }else{
                $images[$id] = DB::select('images', 'id='.$id, __FILE__ . ' line ' . __LINE__);
            }
        }
        return $images[$id];
    }
    public static function getImageByIdFromRedis($id){
        $redis = RedisPhp::getInstance('images');
        $data = $redis->get(IMAGE.$id);
        $data = json_decode($data,true);
        return $data;
    }
    public static function addToRedis($id){
        $redis = RedisPhp::getInstance('images');
        $images = self::getImagesById($id);
        if($images){
            $redis->set(IMAGE.$id,json_encode($images));
            if($images['ref_id'] >0){
                $redis->zAdd(IMAGE_BY_REF_ID.$images['ref_id'],$images['time_c'],$images['id']);
            }
        }

    }
    public static function removeFromRedis($id){
        $redis = RedisPhp::getInstance('images');
        $images = self::getImagesById($id);
        if($images){
            $redis->del(IMAGE.$id,json_encode($images));
            if($images['ref_id'] >0){
                $redis->zREM(IMAGE_BY_REF_ID.$images['ref_id'],$images['id']);
            }
        }
    }
    public static function update($image, $id) {
        return DB::update_id('images', $image, $id, __FILE__ . ' line ' . __LINE__);
    }

    public static function delImageById($id) {
        $image = DB::select_id('images', $id);
        if($image) {
            if(DB::delete_id('images', $id, __FILE__ . ' line ' . __LINE__)) {
                if($image['img_server']) {
                    Upload::storage_delete_file($image['img_url'], IMAGE_SERVER_NO);
                } else {
                    unlink(ROOT_PATH.$image['img_url']);
                }
                return true;
            } else return false;
        } return false;
    }

    public static function delImages($ids) {
        $rs = 0;
        foreach($ids as &$id)
            if($id && self::delImageById($id)) $rs++;
        return $rs;
    }

    public static function have_permit($pids, $id=false) {
        if(User::is_admin()) return true;
        if(User::have_permit($pids)) {
            if(!$id) return true;
            return DB::exists("SELECT * FROM images WHERE id=$id AND user_id=".User::id(), __FILE__ . ' line ' . __LINE__);
        }
        return false;
    }

    public static function checkimg($id)
    {
        $condition = "id=" . $id;
        $c = DB::count('images', $condition, __FILE__ . ' line ' . __LINE__);
        if ($c > 0) {
            return true;
        }else{
            return false;
        }
    }

    //Ham lay width gan nhat tim duoc anh, dung trong file smarty plugin show_image (Tú 04.01.2013)
    public static function getThumbStorage($width = 350)
    {
        /*$width = String::clean($width, Ming_Filter::TYPE_INT, 0);*/
        if (is_int($width)) {
            $arrThumb = array(40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 180, 190, 200, 210, 220,
                230, 240, 250, 260, 290, 300, 320, 350, 360, 380, 400, 430, 480, 500, 550, 570, 640,
                690, 700, 720, 900, 1500, 2000);
            for ($i = 0; $i < count($arrThumb); $i++) {
                if ($arrThumb[$i] >= $width) {
                    break;
                }
            }
            if ($i == count($arrThumb))
                $i = count($arrThumb) - 1;
            $arrThumb[$i];
            $width = $arrThumb[$i];
        }
        return $width;
    }

}
